/**
 * 服务机构列表
 */
$(function(){
    showDepartmentInfo();
    function showDepartmentInfo(){
        layui.use(['table','form'], function(){
            var table = layui.table;
            var form = layui.form;
            //数据表格渲染
            // 信息页面
            var tableIns=table.render({
                elem: '#departmentInfoList'
                ,id:'department'
                ,url:'/department/showDepartmentInfo'
                ,height:390
                ,cellMinWidth: 80//全局定义常规单元格的最小宽度，layui 2.2.1 新增
                ,request:{
                    pageName: 'pn' //页码的参数名称，默认：page
                    ,limitName: 'limit' //每页数据量的参数名，默认：limit
                }

                ,parseData: function(res) { //将原始数据解析成 table 组件所规定的数据
                    return {
                        "code": 0, //解析接口状态
                        "msg": '', //解析提示文本
                        "count": res.total, //解析数据长度
                        "data": res.list, //解析数据列表
                    }
                }
                ,cols: [[
                    // {field:'id', title: 'ID', sort: true,fixed:'left'}
                    {field:'department_name',width:200,title: '护理机构',sort:true,fixed:'left'}
                    ,{field:'department_phone_number',width:150, title: '联系方式'}
                    ,{field:'department_address', width:200,title: '当前地址'}
                    ,{field:'department_settlement_scope',width:150,title: '结算范围'} //minWidth：局部定义当前单元格的最小宽度，layui 2.2.1 新增
                    ,{field:'department_settlement_code',width:150, title: '结算代码',sort:true}
                    ,{field:'department_first_settle_date', width:200,title: '首次可结算日期', sort: true}
                    ,{field:'department_notification_date',width:200, title: '交易代码下发通知书日期',sort:true}
                    ,{field:'department_corporate', width:100,title: '法人'}
                    ,{field:'department_region_code',width:150, title: '区内编码'}
                    ,{field:'department_first_time',width:200, title: '创建时间',sort:true,templet : "<div>{{layui.util.toDateString(d.department_first_time, 'yyyy年MM月dd日 HH:mm:ss')}}</div>"}
                    ,{field:'department_change_time',width:200, title: '修改时间',sort:true,templet : "<div>{{layui.util.toDateString(d.department_change_time, 'yyyy年MM月dd日 HH:mm:ss')}}</div>"}
                    ,{field:'department_status', width:150,title: '状态',sort:true}
                    ,{fixed:'right',toolbar: '#Check_Edit_Del', width:170,title:'操作'}
                ]]
                ,page:true
            });
            // 历史变更页面
            var tableIns2=table.render({
                elem: '#departmentInfoChangedList'
                ,id:'departmentChanged'
                ,url:'/department/showDepartmentChangedInfo'
                ,height:390
                ,cellMinWidth: 80//全局定义常规单元格的最小宽度，layui 2.2.1 新增
                ,request:{
                    pageName: 'pn' //页码的参数名称，默认：page
                    ,limitName: 'limit' //每页数据量的参数名，默认：limit
                }

                ,parseData: function(res) { //将原始数据解析成 table 组件所规定的数据
                    return {
                        "code": 0, //解析接口状态
                        "msg": '', //解析提示文本
                        "count": res.total, //解析数据长度
                        "data": res.list //解析数据列表
                    }
                }
                ,cols: [[
                    {field:'department_name',width:200,title: '护理机构',sort:true}
                    ,{field:'department_settlement_code',width:150, title: '结算代码',sort:true}
                    ,{field:'department_change_time',width:200, title: '修改时间',sort:true,templet : "<div>{{layui.util.toDateString(d.department_change_time, 'yyyy年MM月dd日 HH:mm:ss')}}</div>"}
                    ,{field: 'start_change_date', width: 200, title: '启变日期',sort:true}
                    ,{field:'department_changed_title',width:200,title: '变更字段名称',sort:true}
                    ,{field:'department_history_content',width:300, title: '变更前内容',sort:true}
                    ,{field:'department_new_content',width:300, title: '变更后内容',sort:true}
                    ,{toolbar: '#Check', title: '操作'}
                    // fixed: 'right',
                ]]
                ,page:true
            });
            // 搜索功能
            var $ = layui.$, active = {
                // 信息页面-搜索功能
                reload1: function(){
                    var send_name = $('#department_name');
                    var send_code = $('#department_region_code');
                    var send_status=$('#statusSearch');
                    console.log("搜索的机构是"+send_name.val());
                    console.log("搜索的区内编码是"+send_code.val());
                    console.log("搜索的状态是"+send_status.val());

                    //执行重载
                    table.reload('department', {
                        url: '/department/searchDepartment',
                        method:'post',
                        page: {
                            curr: 1 //重新从第 1 页开始
                        }
                        ,where: {
                            departmentName: send_name.val(),
                            regionCode: send_code.val(),
                            status:send_status.val()
                        }
                    }, 'data');
                },
                Add: function(){
                    console.log("服务机构面");
                    var Data=document.getElementById("AddDepartment");
                    console.log(Data);
                    console.log("新增机构");
                    var data0=$('#AddDepartment')[0].innerHTML.format(Data);
                    layer.open({
                        type: 1,
                        skin: 'layui-layer-demo', //样式类名
                        title: ['基 本 信 息', 'font-size:20px'],
                        closeBtn: 1,
                        area: ['700px', '500px'],
                        shadeClose: true, //开启遮罩关闭
                        btn: ['关闭'],
                        content:data0
                        // $('#ElderDetail')[0].innerHTML.format(data)
                    })
                    form.render('select');
                },
                // 变更页面-搜索功能
                reload2: function(){
                    console.log("触发搜索按钮")
                    var send_name = $('#nameSearch');
                    var send_code = $('#settleCodeSearch');
                    var send_title=$('#titleSearch');
                    console.log("搜索的机构是"+send_name.val());
                    console.log("搜索的结算代码是"+send_code.val());
                    console.log("搜索的字段名称是"+send_title.val());

                    //执行重载
                    table.reload('departmentChanged', {
                        url: '/department/searchChangedInfo',
                        method:'post',
                        page: {
                            curr: 1 //重新从第 1 页开始
                        }
                        ,where: {
                            nameSearch: send_name.val(),
                            settleCodeSearch: send_code.val(),
                            titleSearch:send_title.val()
                        }
                    }, 'data');
                },
            };
            $('.SearchLoadDepartment .searchBtn').on('click', function () {
                console.log("触发按键");
                var type = $(this).data('type');
                console.log("type:"+type);
                active[type] ? active[type].call(this) : '';
                console.log("结束");
            });

            //信息页面的监听（查看\编辑\删除）
            table.on('tool(department)', function(obj){
                var data = obj.data;
                if(data.department_status=="有效"){
                    data.select1="selected";
                    data.select2="";
                }else{
                    data.select2="selected";
                    data.select1="";
                }
                console.log(data);
                var data2=$('#DepartmentDetail')[0].innerHTML.format(data);
                var data3=$('#DepartmentEdit')[0].innerHTML.format(data);
                console.log(data3);
                if(obj.event === 'detail'){
                    console.log("显示详细页面");
                    layer.open({
                        type: 1,
                        skin: 'layui-layer-demo', //样式类名
                        title: ['基 本 信 息', 'font-size:20px'],
                        closeBtn: 1,
                        area: ['700px', '500px'],
                        shadeClose: true, //开启遮罩关闭
                        btn: ['关闭'],
                        content:data2
                        // $('#ElderDetail')[0].innerHTML.format(data)
                    })
                }

                else if(obj.event === 'del'){
                    // System.out.print("选中栏目：id:"+data.id);
                    layer.confirm('是否确认删除？', function(index){
                        $.ajax({
                            url:'/department/delDepartment',
                            method:'post',
                            data:{id:data.id},
                            success:function(){
                                console.log("删除机构信息成功");
                                obj.del();
                                layer.close(index);
                            },
                            error:function(){
                                layer.msg("删除机构信息出错")
                            }
                        })
                    });
                }

                else if(obj.event === 'edit'){
                    console.log("修改");
                    layer.open({
                        type: 1,
                        skin: 'layui-layer-demo', //样式类名
                        title: ['修 改 信 息','font-size:20px;'],
                        closeBtn: 1,
                        area: ['700px', '600px'],
                        shadeClose: true, //开启遮罩关闭
                        content:data3
                    });
                    form.render('select');
                }
            });

            //历史变更页面的监听（查看）
            table.on('tool(departmentChanged)', function(obj){
                var data = obj.data;
                var data2=$('#DepartmentChangedDetail')[0].innerHTML.format(data);
                console.log(data2);
                if(obj.event === 'detail'){
                    console.log("显示详细页面")
                    layer.open({
                        type: 1,
                        skin: 'layui-layer-demo', //样式类名
                        title: ['变更信息', 'font-size:40px'],
                        closeBtn: 1,
                        area: ['700px', '500px'],
                        shadeClose: true, //开启遮罩关闭
                        btn: ['关闭'],
                        content:data2
                    })
                }
            });
        });
    }
});

